package com.example.administrator.myapplication.form;

import android.content.Intent;
import android.os.Parcelable;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.SearchView;
import android.view.View;
import android.widget.Button;

import com.example.administrator.myapplication.R;
import com.example.administrator.myapplication.data.Task;
import com.example.administrator.myapplication.data.source.TaskRepository;
import com.example.administrator.myapplication.data.source.local.TaskLocalDataSource;
import com.example.administrator.myapplication.formdetail.FormDetailActivity;

import java.util.ArrayList;
import java.util.List;

public class FormActivity extends AppCompatActivity implements FormContract.View, View.OnClickListener{

    private static final int REQUEST_ADD = 1001;
    private static final int REQUEST_UPDATE = 1002;

    private FormContract.Presenter mPresenter;
    private SearchView mSearchView;
    private RecyclerView mRecyclerView;
    private Button btnAdd, btnSearch, btnHide;
    private FormRecyclerAdapter mFormRecyclerAdapter;


    private List<Task> taskList = new ArrayList<>();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);


        mFormRecyclerAdapter = new FormRecyclerAdapter(taskList);
        initView();
        mPresenter = new FormPresenter(TaskRepository.getInstance(TaskLocalDataSource.getInstance(this)),
                this);
        mRecyclerView.setLayoutManager(new LinearLayoutManager(this));
        mRecyclerView.setAdapter(mFormRecyclerAdapter);
        mFormRecyclerAdapter.setOnItemClickListener(new FormRecyclerAdapter.OnItemClickListener() {
            @Override
            public void onClick(Task task) {
                goToFormDetailActivityWithUpdate(task);
            }
        });


    }

    @Override
    protected void onResume() {
        super.onResume();
        if (mSearchView.getVisibility() == View.GONE)
            mPresenter.start();
        else {
            mPresenter.startSelected(mSearchView.getQuery().toString());
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (resultCode == 100) {
//            Task taskItem = data.getParcelableExtra("data");
            if (requestCode == REQUEST_UPDATE) {
//                for (int i = 0; i < taskList.size(); i++) {
//                    if (taskList.get(i).getId().equals(taskItem.getId())){
//                        taskList.set(i,taskItem);
//                        mFormRecyclerAdapter.notifyItemChanged(i);
//                    }
//                }

            } else if (requestCode == REQUEST_ADD) {
//                taskList.add(taskItem);
//                mFormRecyclerAdapter.notifyItemInserted(taskList.size()-1);
            }
        }
    }

    private void initView() {
        mSearchView = findViewById(R.id.form_search_view);
        mRecyclerView = findViewById(R.id.form_recycler_view);
        btnAdd = findViewById(R.id.form_add);
        btnSearch = findViewById(R.id.form_search);
        btnHide = findViewById(R.id.form_HIDE);

        btnAdd.setOnClickListener(this);
        btnSearch.setOnClickListener(this);
        btnHide.setOnClickListener(this);
    }

    @Override
    public void setPresenter(FormContract.Presenter presenter) {
//        mPresenter = presenter;
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.form_add:
                goToFormDetailActivityWithAdd();
                break;
            case R.id.form_search:
                if (mSearchView.getVisibility() == View.GONE) {
                    showSearchView();
                } else {
                    String content = mSearchView.getQuery().toString();
                    select(content);
                }

                break;
            case R.id.form_HIDE:
                mSearchView.setVisibility(View.GONE);
                mPresenter.start();
                break;
            default:
                break;
        }
    }

    private void showSearchView() {
        mSearchView.setVisibility(View.VISIBLE);
        mSearchView.onActionViewExpanded();
    }

    private void goToFormDetailActivityWithAdd() {
        Intent intent = new Intent(this, FormDetailActivity.class);
        intent.setAction("add");
        startActivityForResult(intent, REQUEST_ADD);
    }

    private void goToFormDetailActivityWithUpdate(Task taskItem) {
        Intent intent = new Intent(this, FormDetailActivity.class);
        intent.setAction("update");
        intent.putExtra("task", taskItem);
        startActivityForResult(intent, REQUEST_UPDATE);
    }


    @Override
    public void showTasks(List<Task> tasks) {
        taskList = tasks;
        mFormRecyclerAdapter.replaceData(tasks);
    }

    @Override
    public void select(String content) {
        mPresenter.getSelectedTask(content);
    }

    @Override
    public void showSelectedTask(List<Task> tasks) {
//        mFormRecyclerAdapter.replaceData(tasks);
        taskList = tasks;
        mFormRecyclerAdapter.notifyDataSetChanged();
    }


}
